<template>
  <!-- 协催管理-诉讼案件-费用管理 -->
  <section class="component cost-maintain">
    <data-form :model="model" @onSearch="refreshCostData" :page="pageService">
      <template slot="inputs">
        <el-form-item label="费用类别" prop="costType">
          <el-select v-model="model.costType" placeholder="请选择" clearable>
            <el-option v-for="{index,name,code} in $dict.getDictData('CostType')" :key="index" :label="name" :value="code"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="费用项" prop="costEvent">
          <el-select v-model="model.costEvent" placeholder="请选择" clearable>
            <el-option v-for="{index,name,code} in $dict.getDictData('CostEvent')" :key="index" :label="name" :value="code"></el-option>
          </el-select>
        </el-form-item>
      </template>
    </data-form>
    <data-box @onPageChange="refreshCostData" :data="dataSet" :page="pageService">
      <template slot="buttons">
        <el-button @click="dialog.isOpenAddCost=true, rowCostData=null">新增</el-button>
      </template>
      <template slot="columns">
        <el-table-column label="操作" :min-width="$helper.getColumnWidth(4)">
          <template slot-scope="scope">
            <el-button @click="dialog.isOpenAddCost=true, rowCostData=scope.row" type="text">编辑</el-button>
            <el-button @click="deleteCost(scope.row)" type="text">删除</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="costType" label="费用类别" :formatter="(row) => $filter.dictConvert(row.costType,'CostType')" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="costEvent" label="费用项" :formatter="(row) => $filter.dictConvert(row.costEvent,'CostEvent')" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="momey" label="费用金额(元)" :min-width="$helper.getColumnWidth(3)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="payTime" label="缴费时间" :formatter="(row) => $filter.dateFormat(row.payTime)" :min-width="$helper.getColumnWidth(4)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="fileIds" label="付款凭证" :min-width="$helper.getColumnWidth(4)" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-button type="text" @click="rowCostData=scope.row, dialog.isOpenFile=true">查看凭证</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" :min-width="$helper.getColumnWidth(4)" show-overflow-tooltip>
        </el-table-column>
      </template>
    </data-box>

    <!-- 新增/编辑收费记录 -->
    <el-dialog title="收费记录" append-to-body center :visible.sync="dialog.isOpenAddCost" :close-on-click-modal="false" :show-close="false" width="500px" @open="() => this.$nextTick(() => {this.$refs['add-cost'].refresh()})">
      <add-cost ref="add-cost" :rowData="rowData" @close="dialog.isOpenAddCost = false" @refreshCostData="refreshCostData" :rowCostData="rowCostData"></add-cost>
    </el-dialog>

    <!-- 查看凭证 -->
    <el-dialog title="查看凭证" append-to-body center :visible.sync="dialog.isOpenFile" :close-on-click-modal="false" width="500px" @close="dialog.isOpenFile=false" @open="refreshFileData">
      <data-box :data="fileDataSet">
        <template slot="columns">
          <el-table-column label="操作" :min-width="$helper.getColumnWidth(4)">
            <template slot-scope="scope">
              <el-button type="text" @click="download(scope.row.url, scope.row.filename)">下载</el-button>
            </template>
          </el-table-column>
          <el-table-column prop="filename" label="文件名称" :min-width="$helper.getColumnWidth(4)">
          </el-table-column>
        </template>
      </data-box>
    </el-dialog>

  </section>
</template>
<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
import { Dependencies } from "~/core/decorator";
import { OutsourceLawsuitService } from "~/services/domain-service/outsource-lawsuit-manage-index.service";
import { PageService } from "~/utils/page.service";
import DataBox from "~/components/common/data-box.vue";
import DataForm from "~/components/common/data-form.vue";
import AddCost from "~/components/outsource-manage/outsource-lawsuit-manage/lawsuit-list/add-cost.vue";
import { FileUploadService } from "~/services/file-service/file-upload.service";
import { Emit, Prop, Watch } from "vue-property-decorator";

@Component({
  components: {
    
    
    AddCost
  }
})
export default class CostMaintain extends Vue {
  @Dependencies(OutsourceLawsuitService)
  private outsourceLawsuitService: OutsourceLawsuitService;
  @Dependencies(FileUploadService)
  private fileUploadService: FileUploadService;
  @Dependencies(PageService)
  private pageService: PageService;

  @Prop()
  private rowData: any;

  private rowCostData: any = null;

  private model: any = {
    costEvent: "",
    costType: "",
  };

  private dialog: any = {
    isOpenAddCost: false,
    isOpenFile: false,
  };

  private dataSet: any[] = []

  private fileDataSet: any[] = []

  public refreshCostData() {
    this.outsourceLawsuitService.getCostRecord({...this.model, assistId: this.rowData.id}, this.pageService).subscribe(data => {
      this.dataSet = data
    })
  }

  /**
   * 删除
   */
  private deleteCost(row) {
    this.outsourceLawsuitService.deleteCostRecord({ id: row.id }).subscribe(
      data => {
        this.$message.success("删除成功!");
        this.refreshCostData()
      })
  }

  /**
   * 下载文件
   */
  private download(url, filename: string = '') {
    this.$common.downloadFile(url, filename)
  }

  private refreshFileData() {
    this.fileDataSet = []
    const fileIds = this.rowCostData.fileIds
    if (!fileIds) return 
    this.fileUploadService.getAllUploadFileByIds(fileIds).subscribe(
      data => {
        if (!data || data.length === 0) return
        this.fileDataSet = data.map(v => {
          return {filename: v.originalName, id: v.id, url: v.url}
        })
      }
    )
  }
  
  

}
</script>
